package gw.com.android.ui.onlive;

import android.animation.ValueAnimator;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.gwtsz.gts2.hx.R;

import gw.com.android.model.ConfigType;
import gw.com.android.model.ConfigUtil;
import gw.com.android.ui.views.CommonTitleBar;
import gw.com.android.utils.CacheUtils;
import gw.com.android.utils.GuideDialog;
import gw.com.android.utils.PackStatis;
import www.com.library.app.PushMsgTabFragment;

/**
 * @author jett
 * @since 2017-11-17.
 */
public class MainLiveFragment extends PushMsgTabFragment {

    public static final int JIEPAN = 0;
    public static final int DISCUSS = 1;

    private TabLayout tabs;

    private int mCurrPage = DISCUSS;
    private boolean titleBarHidden = false; // 状态栏是否隐藏
    private DiscussFragment discussFragment;
    private JiepanFragment jiepanFragment;
    private CommonTitleBar titleBar;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_main_live;
    }

    @Override
    protected void initLayoutView() {
        titleBar = (CommonTitleBar) mRootView.findViewById(R.id.common_title_bar);
        titleBar.setLeftIcon(0);
        tabs = titleBar.tabs;
        titleBar.tabs.setVisibility(View.VISIBLE);
        tabs.setTabMode(TabLayout.MODE_FIXED);
        tabs.addTab(tabs.newTab().setText(R.string.home_onlive_title), mCurrPage == JIEPAN);
        tabs.addTab(tabs.newTab().setText(R.string.home_quanzi_title), mCurrPage == DISCUSS);
        tabs.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mCurrPage = tab.getPosition();
                if (mCurrPage == DISCUSS) {
                    showDiscussFragment();
                    if (titleBarHidden) {
                        showTitleBar(500);
                    }
                    PackStatis.getToService("clickCircleTab", PackStatis.EventCategory.CIRCLE.getValue(), null, null);
                } else if (mCurrPage == JIEPAN) {
                    showJiepanFragment();
                    PackStatis.getToService("clicktalking", "live", null, null);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
        if (mCurrPage == JIEPAN) {
            showJiepanFragment();
        } else {
            showDiscussFragment();
        }
    }

    @Override
    protected void initViewData() {
    }

    private void showDiscussFragment() {
        FragmentManager fm = getChildFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        if (discussFragment == null) {
            discussFragment = new DiscussFragment();
            ft.add(R.id.fl_content, discussFragment);
        }
        if (jiepanFragment != null) {
            ft.hide(jiepanFragment);
        }
        ft.show(discussFragment);
        ft.commit();
    }

    private void showJiepanFragment() {
        FragmentManager fm = getChildFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        if (jiepanFragment == null) {
            jiepanFragment = new JiepanFragment();
            Bundle bundle = new Bundle();
            bundle.putString(ConfigType.CONFIG_TYPE_KEY_TAG, ConfigType.TAB_ONLIVE_TAG);
            bundle.putString(ConfigType.CONFIG_TYPE_TITLE_TAG, "video");
            bundle.putString("url", ConfigUtil.instance().getUrlPath(ConfigType.TAB_ONLIVE_TAG));
            jiepanFragment.setArguments(bundle);
            ft.add(R.id.fl_content, jiepanFragment);
        }
        if (discussFragment != null) {
            ft.hide(discussFragment);
        }
        ft.show(jiepanFragment);
        ft.commit();
    }

    public void setPage(int page) {
        if (page != DISCUSS && page != JIEPAN) {
            return;
        }
        this.mCurrPage = page;
        if (tabs != null && isAdded()) {
            TabLayout.Tab currTab = tabs.getTabAt(mCurrPage);
            if (currTab != null) {
                currTab.select();
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        //检查是不是第一次来到2.2版本，如果是，就显示引导页
        String flag = CacheUtils.readFile(CacheUtils.CACHE_GUIDE_2_2);
        if (TextUtils.isEmpty(flag)) {//如果flag是空，或者是false
            //  就判定为是第一次
            CacheUtils.writeFile(CacheUtils.CACHE_GUIDE_2_2, "true");
            initGuideDialog();//放在这里测试
        }
    }

    private void initGuideDialog() {
        View layout1 = LayoutInflater.from(getContext()).inflate(R.layout.layout_guide_1, null);
        final GuideDialog dialog1 = new GuideDialog(getContext(), layout1);

        View layout2 = LayoutInflater.from(getContext()).inflate(R.layout.layout_guide_2, null);
        final GuideDialog dialog2 = new GuideDialog(getContext(), layout2);

        layout1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog1.dismiss();
                dialog1.cancel();
            }
        });
        dialog1.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                dialog2.show();// 第一个guide消失以后，第二个马上显示
            }
        });
        layout2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog2.dismiss();
            }
        });
        dialog1.show();
    }

    public void hideTitleBar(long duration) {
        if (titleBarHidden) {
            return;
        }
        if (titleBar != null) {
            int height = titleBar.mAppTitleBar.getHeight() + 1;
            ValueAnimator anim = ValueAnimator.ofInt(0, -height);
            anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    Integer value = (Integer) animation.getAnimatedValue();
                    ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) titleBar.mAppTitleBar.getLayoutParams();
                    params.topMargin = value;
                    titleBar.mAppTitleBar.setLayoutParams(params);
                }
            });
            anim.setDuration(duration);
            anim.start();
            titleBarHidden = true;
        }
    }

    public void showTitleBar(long duration) {
        if (!titleBarHidden) {
            return;
        }
        if (titleBar != null) {
            int height = titleBar.mAppTitleBar.getHeight() + 1;
            ValueAnimator anim = ValueAnimator.ofInt(-height, 0);
            anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    Integer value = (Integer) animation.getAnimatedValue();
                    ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) titleBar.mAppTitleBar.getLayoutParams();
                    params.topMargin = value;
                    titleBar.mAppTitleBar.setLayoutParams(params);
                }
            });
            anim.setDuration(duration);
            anim.start();
            titleBarHidden = false;
        }
    }

    public boolean onBackPressed() {
        if (mCurrPage == DISCUSS) {
            if (discussFragment != null && discussFragment.onBackPressed()) {
                return true;
            }
        }
        if (mCurrPage == JIEPAN) {
            if (jiepanFragment != null && jiepanFragment.onBackPressed()) {
                return true;
            }
        }
        return false;
    }

    public void scrollListToTop() {
        discussFragment.scrollListToTop();
    }

    /**
     * 新手福利小弹窗的广告哇
     * 只有圈子才显示福利小广告哟
     */
    public void showNoviceWelfare(String title, String link) {
        if (discussFragment != null) {
            discussFragment.showNoviceWelfare(title, link);
        }
    }

    public void hideNoviceWelfare() {
        if (discussFragment != null) {
            discussFragment.hideNoviceWelfare();
        }
    }

}
